package ace.cmp.data.methodname.method.name.sql.entity.model;

import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author caspar
 * @date 2023/2/17 14:16
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class EntityMapping {
  private String tableName;

  /**
   * 实体对象的类型
   */
  private Class entityClass;

  /**
   * 解析方法的对象类型
   */
  // private Class methodClass;
  private List<EntityPropertyMapping> properties;

  public Optional<EntityPropertyMapping> getPropertyMappingByPropertyPathMeta(
      String propertyPathMeta) {

    return Stream.ofNullable(this.getProperties())
        .flatMap(List::stream)
        .filter(propertyMapping -> propertyMapping.getPropertyPathMeta().equals(propertyPathMeta))
        .findFirst();
  }

  public Optional<EntityPropertyMapping> getPropertyMappingByProperty(String property) {
    return Stream.ofNullable(this.getProperties())
        .flatMap(List::stream)
        .filter(propertyMapping -> propertyMapping.getProperty().equals(property))
        .findFirst();
  }
}
